import navToNext from '../../../wxjs/navToNext.js'
import Paging from '../../../wxjs/Paging.js'

const app = getApp();

var pag = new Paging();
const tabs = [{
  id: 1,
  text: '推荐',
  status: true,
}, {
  id: 2,
  text: '附近',
  // status: true,
}];

var data = {
  xhx_images: app.xhx_images,
  xhx_route: app.xhx_route,
  tabs: tabs,
  tabId: 1,
  isLoadTab: false, //是否加载tab完成，用来判别首次tab及onshow重复请求
};

var currentPage = {
  data,
  reqUrl: "",
  onLoad,
  getLoadParams,
  adaptNav,
  toggleTab,
  onShow,
  search, //搜索
  openSetting,//打开定位设置
  navToCar,//跳转到购物车
  onShareAppMessage: function () { }
};

var page = Object.assign(pag.func(), navToNext, currentPage);

Page(page)



function onLoad(options) {
  const that = this;
  that.getLoadParams(options);
  that.adaptNav();
}

function getLoadParams(options) {
  const that = this;
  const shareUserId = options && options.shareUserId || "";
  const shareActivityUuid = options && options.shareActivityUuid || "";

  app.shareUserId = shareUserId;
  app.shareActivityUuid = shareActivityUuid;
}

function adaptNav() {
  const that = this;
  const systemInfo = wx.getSystemInfoSync();
  const version = systemInfo.version;
  if (version.slice(0, 1) < 7) {
    that.setData({
      unNavigationStyle: true
    })
    wx.setNavigationBarTitle({
      title: '',
    })
  }
}

function toggleTab(event) {
  const that = this;
  var id = event.detail.id;
  that.setData({
    tabId: id,
    isLoadTab: true,
  })

  if (id == 2) {

    that.setData({
      lists: [], //列表数据
      msg: '', //提示信息
      footMsg: "", //底部提示信息
      noLatitude: false,
    })

    wx.getLocation({
      type: 'gcj02',
      success(res) {
        console.log(res)
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude,
        })
        that.listParams.latitude = res.latitude;
        that.listParams.longitude = res.longitude;
        that.reqUrl = app.serAddrs.recommandNearList
        // 初始化
        that.pageListInit();
      },
      fail: function (res) {
        that.setData({
          noLatitude: true,
        })
      }
    })
  } else {
    app.xlogin.getUser(function (res) { 
      that.listParams.latitude = null;
      that.listParams.longitude = null;
      that.reqUrl = app.serAddrs.recommandList
      // 初始化
      that.pageListInit();
    })
  }
}

function onShow() {
  const that = this;
  const isLoadTab = that.data.isLoadTab;

  if (!app.notifyCount) {
    wx.hideTabBarRedDot({
      index: 2,
    })
  }
}

function search(event) {
  const that = this;
  var keyword = event.detail.value;

  if (event.type == "submit") {
    keyword = event.detail.value.value;
  }

  // 初始化
  that.listDataInit();
  that.listParams.keyword = keyword;
  that.getPageList();
}

function openSetting() {
  const that = this;
  wx.openSetting({
    success(res) {
      if (res.authSetting["scope.userLocation"]) {
        wx.getLocation({
          type: 'gcj02',
          success(res) {
            console.log(res)
            that.setData({
              latitude: res.latitude,
              longitude: res.longitude,
            })
            that.listParams.latitude = res.latitude;
            that.listParams.longitude = res.longitude;
            // 初始化
            that.pageListInit();
          },
          fail: function (res) {
            that.setData({
              noLatitude: true,
            })
          }
        })
      }
    }
  })
}

function navToCar(event) {
  const that = this;

  var myInfo = app.isNavigateToLogin();
  if (!myInfo) return;

  myInfo = app.isNavigateToBind();
  if (!myInfo) return;

  that.navToNext(event);
}